package com.example.hello.service;

import com.example.hello.entity.EmployeeOrderStats;
import com.example.hello.entity.ReportSummary;
import com.example.hello.mapper.ReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportService {

    private static final Logger log = LoggerFactory.getLogger(ReportService.class);

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 获取报表概览数据
     */
    public ReportSummary getReportSummary(LocalDate startDate, LocalDate endDate) {
        // 将日期转换为日期时间
        LocalDateTime startTime = startDate != null ? startDate.atStartOfDay() : null;
        LocalDateTime endTime = endDate != null ? endDate.atTime(LocalTime.MAX) : null;
        
        log.info("获取报表概览数据: startTime={}, endTime={}", startTime, endTime);
        
        return reportMapper.getReportSummary(startTime, endTime);
    }

    /**
     * 获取员工订单统计数据（排行榜）
     */
    public List<EmployeeOrderStats> getEmployeeOrderStats(LocalDate startDate, LocalDate endDate, Integer limit) {
        // 将日期转换为日期时间
        LocalDateTime startTime = startDate != null ? startDate.atStartOfDay() : null;
        LocalDateTime endTime = endDate != null ? endDate.atTime(LocalTime.MAX) : null;
        
        log.info("获取员工订单统计数据: startTime={}, endTime={}, limit={}", startTime, endTime, limit);
        
        return reportMapper.getEmployeeOrderStats(startTime, endTime, limit);
    }
    
    /**
     * 获取完整报表数据（概览数据和员工统计数据）
     */
    public Map<String, Object> getReportDashboard(LocalDate startDate, LocalDate endDate, Integer limit) {
        ReportSummary summary = getReportSummary(startDate, endDate);
        List<EmployeeOrderStats> stats = getEmployeeOrderStats(startDate, endDate, limit);
        
        Map<String, Object> dashboard = new HashMap<>();
        dashboard.put("summary", summary);
        dashboard.put("employeeStats", stats);
        
        return dashboard;
    }
} 